package ecut;

import javax.swing.plaf.nimbus.State;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class TestJdbc1 {
    public static void main(String[] args) throws Exception{
        //1.注册驱动(引入对应的jar包)
        Class.forName("com.mysql.cj.jdbc.Driver");
        System.out.println("注册驱动成功");

        //2.获取数据库连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ecut?serverTimezone=Asia/Shanghai", "root", "123456");
        System.out.println(conn);
        System.out.println("连接成功");

        //3.创建Statement对象
        Statement statement = conn.createStatement();
        //4.执行SQL
        String sql = "select * from user";
        boolean result=statement.execute(sql);
        System.out.println(result);

        //5.处理结果
        ResultSet resultSet=statement.getResultSet();

        /*resultSet.next();
        Object object=resultSet.getObject(2);
        String id=resultSet.getString("id");
        System.out.println(id);*/
        while( resultSet.next() ){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String sex = resultSet.getString("sex");
            System.out.println(id+"  "+name+"  "+"  "+sex);
        }

        //6.释放资源
        resultSet.close();
        statement.close();
        conn.close();

    }
}
